<template>
    <div>
        <el-form ref="form" :model="form" :label-position="labelPosition" style="margin-left:5%">
            <el-form-item label="异议说明 :">
                <el-input type="textarea" v-model="form.declareNote"
                          :autosize="{ minRows: 4, maxRows: 10}"></el-input>
            </el-form-item>
        </el-form>

        <el-table
                :data="fileList"
                border
                style="width: 100%"
                @cell-click="downloadFile">
            <el-table-column
                    prop="name"
                    label="文件名">
            </el-table-column>
            <el-table-column
                    prop="updateDate"
                    label="操作"
                    width="105px">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="text"
                            @click="deleteFile(scope.row)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <div>
            <upload-file @getFileList="uploadFile" style="margin-left: 5%"></upload-file>
        </div>

        <el-dialog
                :title="fileTitle"
                :visible.sync="fileVisible"
                width="95%"
                center>
            <img :src="fileUrl" style="width: 100%"/>
        </el-dialog>
        <div style="text-align: center;width: 100%">
            <el-button @click.native="submit" type="primary" style="width: 80%; margin-top: 5%">提 交</el-button>
        </div>
    </div>
</template>

<script>
    import UploadFile from 'components/upload/upload-button-text.vue'
    import util from 'assets/js/util.js'
    import conf from 'assets/js/conf.js'

    export default {
        data() {
            return {
                labelPosition: 'left',
                isShow: false,
                fileList: [],
                fileTitle: '',
                fileUrl: '',
                fileVisible: false,
                declareFileids: '',
                form: {
                    declareNote: ''
                },
                caseId: 0,
                creditorId: 0
            }
        },

        methods: {
            uploadFile: function (file) {
                if (this.fileList.length > 30) {
                    alert('资料数量太多了, 请于线下邮寄');
                    return;
                }
                this.fileList.push({
                    id: file.id,
                    name: '异议说明 ' + (this.fileList.length + 1),
                    // updateDate: util.formatDate(file.updateDate)
                });
                if (this.declareFileids === '')
                    this.declareFileids = file.id;
                else
                    this.declareFileids = this.declareFileids + ',' + file.id;
            },
            deleteFile: function (row) {
                this.fileList.splice(row.count - 1, 1);
                this.declareFileids = '';
                if (this.fileList.length > 1) {
                    this.fileList.forEach(_file => this.declareFileids += _file.id + ',');
                    this.declareFileids = this.declareFileids.substring(0, this.declareFileids.length - 1);
                }
            },
            downloadFile: function (row, column, cell, event) {
                if (column.label === '操作')
                    return;
                this.fileUrl = conf.baseUrl + '/file/get/' + row.id + '?type=1';
                this.fileTitle = row.name;
                this.fileType = false;
                this.fileVisible = true;
            },
            submit: function () {
                const _self = this;
                util.ajax({
                    url: `/credit/objection`,
                    params: {
                        creditId: util.getUrlParam(document.location.href, 'creditId'),
                        declareNote: this.form.declareNote,
                        declareFileids: this.declareFileids
                    },
                    success: function (data) {
                        _self.$message({
                            message: '提交成功!',
                            center: true
                        });
                        window.location.href = '../creditor/creditor.html?';// window.location.href = '../index/index.html?'+ 'creditId=' + util.getUrlParam(document.location.href, 'creditId');
                    },
                });
            }
        },

        mounted: function () {
            //this.caseId = util.getUrlParam(document.location.href, 'caseId');
            //this.creditorId = util.getUrlParam(document.location.href, 'creditorId');
            this.id = util.getUrlParam(document.location.href, 'creditId');
            this.form.declareNote = decodeURI(util.getUrlParam(document.location.href, 'declareNote'));
            this.declareFileids = util.getUrlParam(document.location.href, 'declareFileids');

            const _self = this;

            util.ajax({
                url: 'file/getFileList',
                params: {eids: util.getUrlParam(document.location.href, 'declareFileids')},
                success: function (data) {
                    data.data.forEach(_file => _self.fileList.push({
                        //updateDate: util.formatDate(_file.updateDate),
                        name: '异议说明 ' + (_self.fileList.length + 1),
                        id: _file.id
                    }))
                },
            });
        },
        components: {
            UploadFile
        }
    }
</script>

<style scoped>

</style>
